import AudioManager from "../core/AudioManager";

const { ccclass, property } = cc._decorator;

export enum TriggerType {
    TouchStart = 0,
    TouchEnd
}

@ccclass
export default class ClickToPlayAudio extends cc.Component {

    @property({ type: cc.AudioClip })
    public audio: cc.AudioClip = null;

    @property({ type: cc.Enum(TriggerType) })
    private triggerType: TriggerType = TriggerType.TouchEnd;

    private static events: string[] = ['touchstart', 'touchend'];

    protected onLoad() {
        this.node.on(ClickToPlayAudio.events[this.triggerType], this.play, this);
    }

    private play() {
        if (this.audio) AudioManager.playEffect(this.audio);
    }

    protected onDestroy() {
        this.node.off(ClickToPlayAudio.events[this.triggerType], this.play, this);
    }
}
